<?php

namespace App\Admin\Metrics\Orders;

use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Metrics\Donut;
use Illuminate\Http\Request;
use Carbon\Carbon;
use App\Models\Order;
use App\Models\Agent;
use Illuminate\Support\Facades\DB;

class DayOrderRaking extends Donut
{
    protected $labels = ['￥0.00', '￥0.00','￥0.00'];

    /**
     * 初始化卡片内容
     */
    protected function init()
    {
        parent::init();

        $color = Admin::color();
        $colors = [$color->primary(), $color->success(),$color->danger()];

        $this->title('今日代理订单金额前三名');
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        $date = Carbon::today();
        $rakings = Order::paid()->whereDate('paid_at', '>=', $date)->groupBy('agent_id')->select(DB::raw("sum(price) as total"),'agent_id')->orderBy('total','desc')->get()->toArray();

        $one = $two = $three ='无';
        if(isset($rakings[0])){
            $username = Agent::find($rakings[0]['agent_id'])->username;
            $this->labels[0] = '￥'.$rakings[0]['total'];
            $one = $username;
        }
        if(isset($rakings[1])){
            $username = Agent::find($rakings[1]['agent_id'])->username;
            $this->labels[1] = '￥'.$rakings[1]['total'];
            $two = $username;
        }
        if(isset($rakings[2])){
            $username = Agent::find($rakings[2]['agent_id'])->username;
            $this->labels[2] = '￥'.$rakings[2]['total'];
            $three = $username;
        }
        $this->withContent($one, $two,$three);
    }
    
    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => $data
        ]);
    }

    /**
     * 设置卡片头部内容.
     *
     * @param mixed $desktop
     * @param mixed $mobile
     *
     * @return $this
     */
    protected function withContent($one, $two,$three)
    {

        $style = 'margin-bottom: 8px';
        $labelWidth = 120;
        $red = Admin::color()->red();
        $green = Admin::color()->success();

        return $this->content(
            <<<HTML
<div class="d-flex pl-1 pr-1 pt-1" style="{$style};font-weight:bold;color:red;">
    <div style="width: {$labelWidth}px">
        <i style="font-style: italic;">1、</i> {$this->labels[0]}
    </div>
    <div>{$one}</div>
</div>
<div class="d-flex pl-1 pr-1" style="{$style};font-weight:bold;color:green;">
    <div style="width: {$labelWidth}px">
        <i style="font-style: italic;">2、</i> {$this->labels[1]}
    </div>
    <div>{$two}</div>
</div>
<div class="d-flex pl-1 pr-1" style="{$style};font-weight:bold;color:blue;">
    <div style="width: {$labelWidth}px">
        <i style="font-style: italic;">3、</i> {$this->labels[2]}
    </div>
    <div>{$three}</div>
</div>
HTML
        );
    }
}
